#!/usr/bin/bash
# ssh-copy-id root@vpn-server01 两个vpn节点免密登录,两个节点都执行
# ssh-copy-id root@vpn-server02 两个vpn节点免密登录,两个节点都执行
# sed -i 's#--status-version\ 2#1\ --status-version\ 3#g' /usr/lib/systemd/system/openvpn-server@.service 修改文件状态日志文件刷新周期为1秒钟
# systemctl daemon-reload && systemctl restart openvpn-server@server # 重启vpn服务
function get_online_user()
{
mkdir -p /tmp/ovpn >/dev/null 2>&1
#ssh root@172.16.50.250 'egrep -i "CLIENT_LIST" /run/openvpn-server/status-server.log|grep 10.' > /tmp/ovpn/ovpn_online_user.txt
ssh root@192.168.50.252 'egrep -i "CLIENT_LIST" /run/openvpn-server/status-server.log|grep 10.' > /tmp/ovpn/ovpn_online_user.txt
}
get_online_user
## 通过ip地址获取地位置信息#############################################################
# vars app_key 在https://tianapi.com中注册中拿到,一天免费查询次数有限，注意要使用qj来处理数据
function whereis()
{
ip_address=$1
if ! [ -x "$(command -v jq)" ]; then
  echo 'The jq package is not installed，Please run dnf install jq to install it.'
  exit 1
fi
#data=$(curl "https://mesh.if.iqiyi.com/aid/ip/info?version=1.1.1&ip=${ip_address}" -s) # -s for slient output
data=$(curl "https://ip9.com.cn/get?ip=${ip_address}" -s) # -s for slient output
status=$(echo $data |jq '.ret' -r)
if [[ $status == "200" ]]; then
  city=$(echo $data |jq '.data' -r | jq '.city' -r)
  province=$(echo $data |jq '.data' -r | jq '.prov' -r)
  country=$(echo $data |jq '.data' -r | jq '.country' -r)
  countryCN=$(echo $data |jq '.data' -r | jq '.isp' -r)
  echo ${country},${province},${city},${countryCN}.
  if [ -z "$countryCN" ] or [ "*" == "$countryCN" ]
  then
  countryCN='null'
  exit 1
  fi
  else
	  echo "API接口不可访问!"
  exit 1
fi
}
# 过滤用户端连接信息##############################################################################
## 这里要以systemd启动ovpn 进程并修改文件/usr/lib/systemd/system/openvpn-server@.service 的值修改为--status-version 2
num=$(cat  /tmp/ovpn/ovpn_online_user.txt |wc -l)
rm -rf /tmp/ovpn/ovpn_online_user_filter.txt >/dev/null 2>&1
touch /tmp/ovpn/ovpn_online_user_filter.txt >/dev/null 2>&1
for i in $(seq 1 $num) 
do
common_name=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user.txt |awk '{print $2}')
real_address=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user.txt |awk '{print $3}'|awk -F ':' '{print $1}')
virtual_address=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user.txt |awk '{print $4}')
Bytes_Received=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user.txt |awk '{printf "%-0.2f\n" ,$5/1024}')
Bytes_Sent=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user.txt |awk '{printf "%-0.2f\n" , $6/1024}')
online_time_tmp=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user.txt |awk '{print $7,$8,$9,$10,$11}')
online_time=$(date -d "$online_time_tmp" +"%Y-%m-%d/%H:%M:%S")
ip_location=$(whereis $real_address)
echo "$common_name $real_address $virtual_address ${Bytes_Received}/${Bytes_Sent} $online_time $ip_location" >>/tmp/ovpn/ovpn_online_user_filter.txt
done

# 获取系统下当前的用户数 ############################################################################
echo "Ovpn_Users Status Real_Address Virtual_Address Received/Sent(KB) Connected_Since Client_Location" >/tmp/ovpn/ovpn_oline_user_time.txt
echo "------------ -------- --------------- --------------- ----------------- ------------------- ------------------------- " >>/tmp/ovpn/ovpn_oline_user_time.txt
##  获取当前系统下签发的证书，通过证书名过滤出common name################################################
find  /etc/openvpn/cerdir/pki/issued/ -name *.crt -exec basename {} .crt \; |grep -v ovpn0 >/tmp/ovpn/all_user.txt

## 基于common name 创建一个模板#######################################################################
num1=$(cat /tmp/ovpn/all_user.txt |wc -l)
for i in $(seq 1 $num1)
do
        str0=$(sed -n "${i}p" /tmp/ovpn/all_user.txt)
        echo "$str0 offline none none none none none" >>/tmp/ovpn/ovpn_oline_user_time.txt
done

# 根据common　name条件来替换内容#########################################################################
numa=$(cat /tmp/ovpn/ovpn_online_user_filter.txt|wc -l)
for i in $(seq 1 $numa)
do
	tmp_name=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user_filter.txt |awk '{print $1}')
	tmp_context=$(sed -n "${i}p" /tmp/ovpn/ovpn_online_user_filter.txt|awk -F' ' '{print $2,$3,$4,$5,$6}')
	sed -i "s#^${tmp_name}.*#${tmp_name}\ online\ ${tmp_context}#g" /tmp/ovpn/ovpn_oline_user_time.txt
done
# 格式化输出文件内容############################################################################################
awk -F' ' '{printf "%-14s %-10s %-16s %-16s %-18s %-20s %-20s\n" ,$1,$2,$3,$4,$5,$6,$7}' /tmp/ovpn/ovpn_oline_user_time.txt
